/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.core;

import com.rw.magallanes.db.Storage;
import com.rw.magallanes.db.StorageException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Gonzalez
 */
@XStreamAlias("Warehouse")
public class Warehouse {

    private List<Category> categories;

    public Warehouse() {
        this.categories = new ArrayList<Category>();
    }

    public void addCategory(Category category) {
        int index = Collections.binarySearch(categories, category);
        if (index < 0) {
            index = -index - 1;
        }
        this.categories.add(index, category);
    }

    public void addItem(Category category, Item item) {
        category.addItem(item);
        item.setCategory(category);
    }

    public List<Category> getCategories() {
        return categories;
    }

    public List<Item> getAllItems() {
        List<Item> all = new ArrayList<Item>();
        for (Category c : categories) {
            all.addAll(c.getItems());
        }
        return all;
    }

    public void deleteItem(Item item) {
        Category category = item.getCategory();
        category.deleteItem(item);
    }

    public void deleteCategory(Category category) {
        List<Item> items = category.getItems();
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            iterator.remove();
        }
        this.categories.remove(category);
    }

    public boolean existsCategoryName(String name) {
        for (Category category : categories) {
            if (category.getName().compareTo(name) == 0) {
                return true;
            }
        }
        return false;
    }

    public boolean existsItemName(String name) {
        for (Category category : categories) {
            List<Item> items = category.getItems();
            for (Item item : items) {
                if (item.getName().compareTo(name) == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public Category getCategory(String categoryName) {
        for (Category category : categories) {
            if (category.getName().compareTo(categoryName) == 0) {
                return category;
            }
        }
        return null;
    }

    public void clear() {
        for (Category category : categories) {
            category.getItems().clear();
        }
        this.categories.clear();
    }

    public void save() throws StorageException {
        Storage.getInstance().save();
    }

    public Category[] getCategoriesArray() {
        return this.categories.toArray(new Category[0]);
    }
}
